package p.ithorns.framework.crypto.core;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import p.ithorns.framework.crypto.utils.RSAUtils;

import java.util.Map;

/**
 * CryptoProperties
 *
 * @author ithorns
 * @version 1.0
 * @date 2019/5/24 12:15
 */
@ConfigurationProperties(prefix = "spring.crypto")
public class CryptoProperties implements InitializingBean {

    /**
     * 私钥
     */
    private String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvXtRK9eYhi9dwk9wwtFQQoPjuuBT2lb4P/8gFQLf130i+qdCjuXJIrcnMKR2VrBmbX0ee+W+M2dOer5EEwqSG7NElalTYXKUaH5A1mlmVe7CTaTcWAB34z92y1t2DHavkVjoetu6AQZ47sV1Z9Y5NeTOJgslOpgzoclJdlSbn1AgMBAAECgYACWfgzfngisPdNYlm9HKzzyvFw83YnGTcM0yrwlraPQoqIPqeBYLCxzIU6JfwZWnlbFVn0OaE16t6AXsWRyw2s+x9u1wb7Zn6NKqMrMvOQ7KH5WdF+deqS1bXyZPE1YsqeMcjSFI7XMTEwH7sDT1+BKXGRqngr93IGvLcrUSRuQQJBANProIqvwi4FmuFo2ck9+/cpKJnEgxGtQAiAjZaYQlxidmQaYGxea5nV2i9eyUKEYh/za8OQ2H0jHpEoyX9JTekCQQCo7hG0/YNdTZBtQaDFH8IZf57p7CgV1h6CoSBszbxiq1wJfmf6/nuKdYYQVik/yxcBUNe1VAW3S59SS70ZocgtAkBi1pSr2klweYDA/MwGhfhMUE/1V6fhyLICxSJESIM631lkWsD6CheZShpW2Zh4g5HYYQmzzoyJ4KVTAnjn+uJxAkAmd0dK6isedMn9RuNf35JoEfpSk7aw+jeqURw8CkDqStDDTKhKq+XfqlEjqSrtXsZXQ1xK6t3+Vdbqe1qM2pzlAkA5zsfFoE4DnDKDQwrERwECIqA8tYprZI5DqoS81/FlD0AWoDL68/G9uJUdOAwIRJWElpP+B9rU1xJPFTi1dKPM";

    /**
     * 公钥
     */
    private String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL17USvXmIYvXcJPcMLRUEKD47rgU9pW+D//IBUC39d9IvqnQo7lySK3JzCkdlawZm19HnvlvjNnTnq+RBMKkhuzRJWpU2FylGh+QNZpZlXuwk2k3FgAd+M/dstbdgx2r5FY6HrbugEGeO7FdWfWOTXkziYLJTqYM6HJSXZUm59QIDAQAB";

    /**
     * 字符集
     */
    private String charset = "UTF-8";

    /**
     * 开启调试模式，调试模式下不进行加解密操作，用于像Swagger这种在线API测试场景
     */
    private boolean debug;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (privateKey == null && publicKey == null) {
            Map<String, Object> keys = RSAUtils.genKeyPair();
            this.privateKey = RSAUtils.getPrivateKey(keys);
            this.publicKey = RSAUtils.getPublicKey(keys);
        }
    }


    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }
}
